A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

While vs Repeat Differences


To tell the difference between a repeat versus a while loop, consider the following program. The two outputs will be different:
program Project2;
{$APPTYPE CONSOLE}
uses SysUtils;

var
  i: integer;
  x: char;
  s: string[10];
begin
  s:='5blackcats';
  i:= 0;
  x:='t';
  writeln('While loop output:');
  while (x <> 't') do
  begin
    inc(i);
    x:= s[i];
    write(s[i]);
  end;

  s:='5blackcats';
  i:= 0;
  x:='t';
  writeln;
  writeln('Repeat loop output:');
  repeat
    inc(i);
    x:= s[i];
    write(s[i]);
  until x = 't';

  readln;
end.
The while loop checks that x is equal t and stops immediately. The repeat loop runs through the first time without checking x first, because it checks x inside the loop only.

About
This site is about programming and other things.
_ _ _